// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
	async userlistAction(){
		this.meta_title = '分店APP用户管理';
		if(this.isPost){
			let ulist = await this.model('or_app_user')
						.alias("ou")
						.field('ou.id,ou.username,ou.name as nickname,ou.phone,ou.type,ou.zid,ou.s_arr,ou.u_arr,og.name,og.code,ou.status,ap.name as pname')
						.join("think_organization og ON ou.o_id = og.id")
						.join('think_app_position ap on ou.zid = ap.id')
						.where({o_id:this.shop_userinfo.o_id,s_id:this.shop_userinfo.s_id})
						.select();
			let relist = {
            "draw": '',
            "data": ulist,
            "breadcrumb":''
        	}
			return this.json(relist);
		}
		return this.display();	
	}
	
	async adduserAction(){
		if(this.isPost){
			let is_exist = await this.model('or_app_user').where({o_id:this.shop_userinfo.o_id,username:this.post('username')}).find();
			if(!think.isEmpty(is_exist)){
				return this.fail('该登陆账号已经存在！');
			}
			let data = this.post();
			data['o_id'] = this.shop_userinfo.o_id;
			data['o_code'] = this.shop_userinfo.o_code;
			data['s_id'] = this.shop_userinfo.s_id;
			data['u_arr'] = data['u_arr']?data['u_arr'].constructor == Array?data['u_arr'].join(','):data['u_arr']:'';
			data['password'] = think.md5(data['password']);
			let res = await this.model('or_app_user').add(data);
			return res?this.success({info:"添加成功！"}):this.fail('添加失败！');
		}
		//获取职位信息
		let plist = await this.model('app_position').where('s_num<=1').select();
		//获取店家下app用户信息
		let ulist = await this.model('or_app_user').alias('oa').field('oa.id,oa.username').join('think_app_position ap on oa.zid=ap.id').where({s_id:this.shop_userinfo.s_id,auth:0}).select();
		let data = {
			'plist':plist,
			'ulist':ulist,
		};
		return this.json(data);
	}
	
	async edituserAction(){
		if(this.isPost){
			let data = this.post();
			let is_exist = await this.model('or_app_user').where({o_id:this.shop_userinfo.o_id,username:data.username,id: ["!=",data.id]}).find();
			if(!think.isEmpty(is_exist)){
				return this.fail('该登陆账号已经存在！');
			}
			data['u_arr'] = data['u_arr']?data['u_arr'].constructor == Array?data['u_arr'].join(','):data['u_arr']:'';
			data['password']?data['password'] = think.md5(data['password']):delete data['password'];
			let res = await this.model('or_app_user').where({id:data.id}).update(data);
			return res?this.success({info:"编辑成功！"}):this.fail('编辑失败！');
		}
		
		let id = this.get('id');
		//取出用户信息	
		let uinfo = await this.model('or_app_user').where({id:id}).find();
		let u_arr = uinfo.u_arr?uinfo.u_arr.split(','):uinfo.u_arr;		
		let plist = await this.model('app_position').where('s_num<=1').select();		
		//获取用户列表
		let ulist = await this.model('or_app_user').alias('oa').field('oa.id,oa.username').join('think_app_position ap on oa.zid=ap.id').where({s_id:this.shop_userinfo.s_id,auth:0}).select();		
		let data = {
			"uinfo":uinfo,
			"u_arr":u_arr,
			"plist":plist,
			"ulist":ulist,
		};
		return this.json(data);	
	}
	
	async deleteuserAction(){
		let id = this.post('id');
		let res = await this.model('or_app_user').where({id:id}).delete();
		return res?this.success({info:"删除成功！"}):this.fail('删除失败！');
	}
	
	
	
  
}